define(['webcam'],function(Webcam){

    var CamControl =  (function () {
        var setImg = function (opt) {
            if(opt.after) {switchToImg(opt.after,"#afterOperate"); }
            if(opt.before) {switchToImg(opt.before,"#beforeOperate"); }
        }
        var init =function(opt) {
            var $ifr = null;
            if(opt && opt.model == "readOnly" ) {
                // 控制显示
                $(".btn-p-capture").hide();
                $(".btn-p-remove").hide();
                $(".runCam").hide();

            }
            if(opt && opt.after) {switchToImg(opt.after,"#afterOperate"); }
            if(opt && opt.before) {switchToImg(opt.before,"#beforeOperate"); }
            $(".btn-p-save").click(function (e) {
                var targetArea =  getCurrentArea(this);
                var $img =  $(targetArea ).find(".cam-preview");
                download($img.attr("src"));
            });
            $(".btn-p-capture").click(function(e) {
                // 采集
                var targetArea =  getCurrentArea(this);
                var beforeOrAfter =  targetArea.indexOf("after") <0?0:1;
                takeSnapshot(targetArea,beforeOrAfter);
            });
            $(".btn-p-remove").click(function() {
                var targetArea =  getCurrentArea(this);
                var beforeOrAfter =  targetArea.indexOf("after") <0?0:1;
                var $img =  $(targetArea ).find(".cam-preview");
                $img.addClass("hide");
                switchTipAndCam(targetArea);
                m.onRemove(beforeOrAfter);
            });
            $(".runCam").click(function (e){
                var targetArea =  $(this).attr("rel");
                switchTipAndCam(targetArea);
            })
        }

        function download (src) {
        }
        function getCurrentArea (t) {
            return "#" + $(t).parents(".panel-box").attr("id");
        }

        function camOnClose () {
            Webcam.reset(); //remove
        }
        function switchToImg (data_uri,targetArea) {
            var $img =  $(targetArea ).find(".cam-preview");
            $img.attr("src",data_uri);
            $img.removeClass("hide");
            var $stopBtn =  $(targetArea).find(".stop-btn");
            var $cam =  $(targetArea).find(".cam-area");
            var $tip =  $(targetArea).find(".default-info");
            $stopBtn.addClass("hide");
            $cam.addClass("hide");
            $tip.addClass("hide");
        }
        function takeSnapshot(targetArea) {
            Webcam.freeze();
            var beforeOrAfter =  targetArea.indexOf("after") <0?0:1;
            setTimeout(function () {
                Webcam.snap( function(data_uri) {
                    //1 显示
                    switchToImg(data_uri,targetArea);
                    // 2上传
                    m.onCapture(data_uri,beforeOrAfter);
                });
            },700)

        }

        var switchTipAndCam = function (targetArea) {
            var $tip =  $(targetArea).find(".default-info");
            var $stopBtn =  $(targetArea).find(".stop-btn");
            var $cam =  $(targetArea).find(".cam-area");
            if($tip.hasClass("hide")) {
                $tip.removeClass("hide");
                // 摄像头相关 dom
                $stopBtn.addClass("hide");
                $cam.addClass("hide");
                camOnClose();
                return;
            }
            $tip.addClass("hide");
            $stopBtn.removeClass("hide");
            $cam.removeClass("hide");
            createCam(targetArea);
        }
        var createCam = function (targetArea) {
            Webcam.set({
                width: 420,
                height: 350,
                dest_width: 800,
                dest_height: 700,
                image_format: 'jpeg',
                jpeg_quality: 90
            });
            Webcam.attach(targetArea + ' .cam-area');
        }
        var m = {
            onCapture : function (cb) {
                if(typeof cb == "function") {
                    m.onCapture = cb;
                }
            },
            onRemove : function (cb) {
                if(typeof cb == "function") {
                    m.onRemove = cb;
                }
            },
            setImg:setImg,
            init: init

        };
        return m;
    })();

    return CamControl;

});